﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    /// <summary>
    /// 种母断奶仓储层实现
    /// </summary>
    public class SheepAblactationRepository : ISheepAblactationRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext _context;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"> 上下文 </param>
        public SheepAblactationRepository(AgricultureDbContext context)
        {
            _context = context;
        }
        /// <summary>
        /// 获取档案表母羊在哺乳的耳号
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheepfilemodel>> GetEweEarTagsFromSheepFile()
        {
            var list = await _context.Sheepfilemodels.ToListAsync();
            return list;
        }
        /// <summary>
        /// 获取羊只的栋舍
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheepfilemodel>> GetBuildByEar()
        {
            var list = await _context.Sheepfilemodels.ToListAsync();
            return list;
        }
        /// <summary>
        /// 添加种母断奶
        /// </summary>
        /// <param name="sheepablactation"> 种母断奶 </param>
        /// <returns> 返回受影响的行数 </returns>
        public async Task<int> AddSheepAblactation(List<Sheepablactation> sheepablactation)
        {
            await _context.Sheepablactations.AddRangeAsync(sheepablactation);
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 种母断奶仓储实现
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Sheepablactation>> GetSheepAblactations()
        {
            var sheepList = await _context.Sheepablactations.ToListAsync();
            return sheepList;
        }
        /// <summary>
        /// 逻辑删除种母断奶
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelSheepAblacations(int userId, params int[] ids)
        {
            if (ids.Length <= 0)
            {
                //没有要删除的数据
                return 0;
            }
            var sheepablactations = await _context.Sheepablactations.Where(x => ids.Contains(x.AblactationId)).ToListAsync();
            foreach (var sheepablactation in sheepablactations)
            {
                sheepablactation.IsDel = true;
                sheepablactation.UpdateTime = DateTime.Now;
                sheepablactation.UpdateName = _context.Userinfos.FirstOrDefault(p => p.Uid == userId).Uname;
            }
            return await _context.SaveChangesAsync();
        }
    }
}
